<?php

namespace app\yj\controller;

use app\yj\model\DataModel;
use app\yj\model\QuyuModel;
use app\yj\model\RoleModel;
use app\yj\model\UserModel;
use think\Controller;
use think\Db;
use think\Request;

class UserController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->get();
        $pageindex = array_key_exists('index', $data) ? $data['index'] : 1;
        $size = array_key_exists('size', $data) ? $data['size'] : 10;

        $name = $request->get('name');
        $roleid = $request->get('roleid');
        $qyid = $request->get('qyid');

//        $users = UserModel::select();

        //先查询下面的所有区域级别
//        $users = Db::table('bh_quyu')
//            ->alias('q')
//            ->where('q.fid', 'like', "$quyufid%")
//            ->join('bh_user u', 'u.qyid = q.id')
//            ->where("length(q.fid) > $len ")
//            ->limit(($pageindex - 1) * $size, $size)
//            ->select();
//        $count = Db::table('bh_quyu')
//            ->alias('q')
//            ->where('q.fid', 'like', "$quyufid%")
//            ->join('bh_user u', 'u.qyid = q.id')
//            ->where("length(q.fid) > $len ")
//            ->count();
        $usersql = UserModel::limit(($pageindex - 1) * $size, $size)->whereNotLike('username', 'root');
        if ($name) $usersql->where('username', 'like', '%' . $name . '%');
        if ($roleid) $usersql->where('roleid', $roleid);
        if ($qyid) $usersql->where('qyid', $qyid);

        $users = $usersql->select();
        foreach ($users as $v) {
            $quyu = QuyuModel::find($v->qyid);
            $role = RoleModel::find($v->roles);
            $v['quyu'] = $quyu ? $quyu->name : '暂无区域';
            $v['quyuobj'] = $quyu;
            $v['role'] = $role ? $role->name : '暂无角色';
        };
        $count = $usersql->count();

        return $this->SuccessReturn('ok', $users, $count);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $user_model = new UserModel();
        //
        $data = $request->post();
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $username = array_key_exists('username', $data) ? $data['username'] : '';
        $password = array_key_exists('password', $data) ? $data['password'] : '';
        $qyid = array_key_exists('qyid', $data) ? $data['qyid'] : 0;
        $mapurl = array_key_exists('mapurl', $data) ? $data['mapurl'] : '';
        $phone = array_key_exists('phone', $data) ? $data['phone'] : '';

        $roles = array_key_exists('roles', $data) ? $data['roles'] : '';
        if ($username == '' || $password == '' || $name == '') {
            return $this->ErrorReturn('400', '缺少必要参数');
        }
        //检查是否存在相同的用户名
        $issetuser = $user_model->where('username', $username)->find();
        if ($issetuser) return $this->ErrorReturn('203', '已存在相同的用户名，请重新创建');

        $new_user = new UserModel();
        $new_user->username = $username;
        $new_user->password = $password;
        $new_user->name = $name;
        $new_user->qyid = $qyid;
        $new_user->roles = $roles;
        $new_user->mapurl = $mapurl;
        $new_user->phone = $phone;
        $new_user->save();
        $role = RoleModel::find($roles);
        $new_user->role = $role ? $role->name : '暂无角色';
        $quyu = QuyuModel::find($qyid);
        $new_user->quyu = $quyu ? $quyu->name : '暂无区域';
        return $this->SuccessReturn('添加成功', $new_user);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->put();
        $id = array_key_exists('id', $data) ? $data['id'] : 0;
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $username = array_key_exists('username', $data) ? $data['username'] : '';
        $password = array_key_exists('password', $data) ? $data['password'] : '';
        $roles = array_key_exists('roles', $data) ? $data['roles'] : '';
        $qyid = array_key_exists('qyid', $data) ? $data['qyid'] : '';
        $phone = array_key_exists('phone', $data) ? $data['phone'] : null;
        $mapurl = array_key_exists('mapurl', $data) ? $data['mapurl'] : null;

        if ($id == 0) return $this->ErrorReturn(400, '缺少用户id');
        //验证是否存在用户
        $user = UserModel::where('id', $id)->find();
        if (!$user) return $this->ErrorReturn(400, '不存在用户');

        //改变用户
        if ($name != '') $user->name = $name;
        if ($username != '') {
            //插找是否有相同的用户名
            $issetuser = UserModel::where('username', $username)->find();
            if ($issetuser) return $this->ErrorReturn(300, '存在相同的用户名 请重新修改');
            $user->username = $username;
        }

        if ($password != '' && $password != null) $user->password = $password;
        if ($qyid != '') {
            $qyid = $qyid[0];
            $user->qyid = $qyid;
        }

        if ($phone != null) $user->phone = $phone;
        if ($mapurl != null) $user->mapurl = $mapurl;

        if ($roles != '') $user->roles = $roles;
        $res = $user->save();
        $role = RoleModel::find($roles);
        $user->role = $role ? $role->name : '暂无角色';
        $quyu = QuyuModel::find($qyid);
        $user->quyu = $quyu ? $quyu->name : '暂无区域';
        $user->quyuobj = $quyu;
        if ($res) return $this->SuccessReturn('修改成功', $user);
        return $this->ErrorReturn(500, '服务器错误 修改失败');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        $id = $request->delete('id');
        if ($id == 0) return $this->ErrorReturn(400, '用户id无效');
        //查询是否存在此用户
        $user = UserModel::where('id', $id)->find();

        if (!$user) return $this->ErrorReturn(404, '找不到此用户');
//        $data = DataModel::where('uid', $user['id'])->find();
//        if ($data) return $this->ErrorReturn(400, '当前用户存在数据 请先处理数据');
        $user->delete();
        return $this->SuccessReturn('删除成功');
    }

    //修改个人信息
    public function editInfo(Request $request)
    {
        $id = $request->put('id');
        if (!$id) return $this->ErrorReturn(302, '缺少id参数');

        $all = $request->put();
        $name = key_exists('name', $all) ? $all['name'] : '';
        $password = key_exists('password', $all) ? $all['password'] : '';
        $avator = key_exists('avator', $all) ? $all['avator'] : '';
        $phone = array_key_exists('phone', $all) ? $all['phone'] : null;
        $mapurl = array_key_exists('mapurl', $all) ? $all['mapurl'] : null;
        $now_u = UserModel::where('id', $id)->find();
        if (!$now_u) return $this->ErrorReturn(500, '找不到对应的用户');

        if ($name) $now_u->name = $name;
        if ($password) $now_u->password = $password;
        if ($avator) $now_u->avator = $avator;
        if ($phone != null) $now_u->phone = $phone;
        if ($mapurl != null) $now_u->mapurl = $mapurl;
        $now_u->save();
        return $this->SuccessReturn('修改成功', $now_u);

    }

    public function userdw(Request $request)
    {
        $id = $request->post('id');
        $latlng = $request->post('latlng');
        if ($id) {
            $user = UserModel::find($id);
            if ($user) {
                $user->latlng = $latlng;
                $user->save();
                return $this->SuccessReturn('ok', $user);
            }
        }
        return $this->ErrorReturn(500, '不存在用户');

    }

    public function findUser(Request $request)
    {
        $id = $request->post('id');
        if ($id) {
            $user = UserModel::find($id);
            return $this->SuccessReturn('ok', $user);
        }
    }

}
